<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
use Think\Controller;
class PosterController extends BaseController {

    public $local_id;

    public function __construct()
    {
        parent::__construct();
        $this->_arr['title'] = '微信设置';
        $this->local_id = session('userInfo')['local_id'];
    }
    public function index(){
       $data = M('weixin_poster')->where(['local_id'=>$this->local_id])->find();
        $this->assign('ruleInfo',$data);
        $this->_showDisplay();
    }




    public function add(){
        $images = I('images');
        if(count($images)>1){
            $this->error('只允许传一张背景图');
        }

        $ruledata = $_POST['Poster'];
        if(empty($ruledata['title'])){
            $this->error('海报标题不能为空');
        }

        if(empty($ruledata['content'])){
            $this->error('海报语言不能为空');
        }
        if(empty($images)){
            $this->error('请上传一张背景图');
        }

        $ruledata['local_id'] = $this->local_id;

        try{
            // 添加image图片
            if (is_array($images) && !empty($images)) {
                $ruledata['poster_pic'] = $images[0];
            }
            if(empty($ruledata['id'])){

                $poster_add = M('weixin_poster')->add($ruledata);
                if($poster_add === false){
                    throw new \Exception('海报设置失败！');
                }
                $this->success('添加成功',U('Poster/index'));
            }else{
                $poster_save = M('weixin_poster')->where(['id'=>$ruledata['id']])->save($ruledata);
                if($poster_save === false){
                    throw new \Exception('海报设置更改失败！');
                }
                //更新成功 将生成海报的用户end_time 设置为当前
                $poster_save_time = M('distribution_member')->where(['end_time'=>['neq','']])->save(['end_time'=>date('Y-m-d H:i:s',time())]);
                if($poster_save_time === false){
                    throw new \Exception('海报设置更改失败！');
                }

                $this->success('更改成功',U('Poster/index'));
            }

        }catch (\Exception $e){
            $this->error('错误：' . $e->getMessage());
        }

    }


  

    

   

}